package com.cloudkinto.controller.file;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.file.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020/5/27 11:20
 * @description:
 */
@Api(tags = "附件上传")
@RequestMapping("/api/file/upload")
@RestController
public class FileController extends BaseController {
    @Autowired
    private FileService service;


//    @UserSessionRequired
    @ApiOperation(value = "附件上传", httpMethod = "POST")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public SingleResult uploadFile(@RequestParam(value = "file", required = true) MultipartFile file) {
        Long userId = null;
        return service.uploadTempFile(file,userId);
    }

    @UserSessionRequired
    @ApiOperation(value = "根据fileId获取临时目录文件", httpMethod = "GET")
    @RequestMapping(value = "/getFileUrl", method = RequestMethod.GET)
    public SingleResult getFileUrl(@RequestParam Long fileId) {
        return SingleResult.success(service.getFilePathTemp(fileId));
    }

    /*@ApiOperation(value = "图片上传base64", httpMethod = "POST")
    @RequestMapping(value = "/uploadImageBase64", method = RequestMethod.POST)
    public SingleResult uploadImage(String base64,String fileName) throws Exception {
        return service.uploadImage(base64,fileName);
    }*/

   /* @ApiOperation(value = "获取图片", httpMethod = "POST")
    @RequestMapping(value = "/downLoadImage", method = RequestMethod.POST)
    public SingleResult downLoadImage(String fileName) throws Exception {
        return service.downLoadImage(fileName);
    }*/


//    @UserSessionRequired
//    @ApiOperation(value = "多附件上传", httpMethod = "POST")
//    @RequestMapping(value = "/uploadMatchFile", method = RequestMethod.POST)
//    public SingleResult uploadMatchFile(@RequestParam("files") MultipartFile[] files) {
//        Long userId = getCurrentUserId();
//        List<Long> fileIds = new ArrayList<>();
//        if(files.length == 0){
//            throw new BizException("请选择文件");
//        }
//        for (MultipartFile file : files) {
//            Map map = (Map) service.uploadTempFile(file,userId).getData();
//            fileIds.add((Long) map.get("fileId"));
//        }
//        return SingleResult.success(fileIds);
//    }

//
//    @UserSessionRequired
//    @ApiOperation(value = "测试", httpMethod = "POST")
//    @RequestMapping(value = "/uploadRealFile", method = RequestMethod.POST)
//    public SingleResult uploadRealFile(@RequestParam Long fileId) {
//        service.uploadFile(fileId);
//        return SingleResult.success();
//    }
}
